package com.lscdu.gulimall.ruoyi_springboot.service.impl;

import com.lscdu.gulimall.ruoyi_springboot.bean.login.LoginUser;
import com.lscdu.gulimall.ruoyi_springboot.bean.login.SysUser;
import com.lscdu.gulimall.ruoyi_springboot.service.ISysUserService;
import com.lscdu.gulimall.ruoyi_springboot.service.SysPermissionService;
import com.lscdu.gulimall.ruoyi_springboot.utis.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author 啊昌
 * @date 2024/9/9 15:26
 * UserDetailsServiceImpl 用户验证处理
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {


	@Autowired
	private ISysUserService sysUserService;


	@Autowired
	private SysPermissionService permissionService; //用户权限


	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		//查询用户
		SysUser user = sysUserService.selectUserByUserName(username);
		if(StringUtils.isNull(user)){
			// todo 没有该用户 则抛出异常  还可以根据字段判断用户是否被停用或者删除
		}
		// todo 缓存密码错误的次数

		// 放行用户
		LoginUser loginUser = new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));
		return loginUser;
	}
}
